package com.zxq.uart.mqtt;

import com.zxq.factory.FastFrame;
import com.zxq.uart.fegin.client.ShiLuWenGanFeignClient;
import com.zxq.uart.frame.UartFrame;
import com.zxq.uart.frame.decoder.*;
import com.zxq.uart.frame.encoder._05FillEndResult;
import com.zxq.uart.frame.encoder._0DDeviceErrorResult;
import com.zxq.uart.frame.encoder._23CardPreConsumerResult;
import com.zxq.uart.lock.LockContainer;
import lombok.extern.slf4j.Slf4j;
import org.apache.xmlbeans.impl.util.HexBin;
import org.eclipse.paho.client.mqttv3.*;

/**
 * @author haungfeilong@wingtech.com
 * @version 1.0
 * @ClassName PushCallback
 * @description
 * @date 2020/11/26 17:50
 * @since JDK 1.8
 */
@Slf4j
public class MQTTReceiveCallback implements MqttCallback {


    private ChargeMqttClient chargeMqttClient;
    private ShiLuWenGanFeignClient feignClient;

    public MQTTReceiveCallback(ChargeMqttClient chargeMqttClient, ShiLuWenGanFeignClient feignClient) {
        this.chargeMqttClient = chargeMqttClient;
        this.feignClient = feignClient;
    }



    @Override
    public synchronized void connectionLost(Throwable throwable) {
        log.info("connectionLost");
    }


    @Override
    public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {

        if (!topic.contains("device/")) {
            log.info("非法消息主题:{},不做处理", topic);
            return;
        }
        //设备下行逻辑处理
        String devId = topic.substring(topic.indexOf("/") + 1);
        String responseTopic = "service/" + devId;
        System.out.println("devId:" + devId);
        System.out.println("responseTopic:" + responseTopic);
        UartFrame resultFrame = new UartFrame();
        byte[] recMsg = mqttMessage.getPayload();
        Integer cmd = Integer.valueOf(recMsg[2]);
        switch (cmd) {
            case 0x05: {
                _05FillEndCmd fillEndCmd = new _05FillEndCmd();
                resultFrame.setData(fillEndCmd);
                FastFrame.decoder(resultFrame, recMsg);
                fillEndCmd.setDevId(devId);
                _05FillEndResult fillEndResult = feignClient._05FillEndCmd(fillEndCmd);
                resultFrame.setData(fillEndResult);
                chargeMqttClient.publishMessage(responseTopic,FastFrame.encoder(resultFrame));
            }
                break;
            case 0x0D:{
                _0DDeviceErrorReport deviceErrorReport = new _0DDeviceErrorReport();
                resultFrame.setData(deviceErrorReport);
                FastFrame.decoder(resultFrame,recMsg);
                deviceErrorReport.setDevId(devId);
                _0DDeviceErrorResult dDeviceErrorResult = feignClient._0DDeviceErrorReport(deviceErrorReport);
                resultFrame.setData(dDeviceErrorResult);
                chargeMqttClient.publishMessage(responseTopic,FastFrame.encoder(resultFrame));
            }
            break;
            case 0x23:{
                _23CardConsumerReport cardConsumerReport = new _23CardConsumerReport();
                resultFrame.setData(cardConsumerReport);
                FastFrame.decoder(resultFrame,recMsg);
                cardConsumerReport.setDevId(devId);
                _23CardPreConsumerResult cardPreConsumerResult = feignClient._23CardConsumerReport(cardConsumerReport);
                resultFrame.setData(cardPreConsumerResult);
                chargeMqttClient.publishMessage(responseTopic,FastFrame.encoder(resultFrame));
            }
            break;
            case 0x03:{
                _03CoinReport coinReport = new _03CoinReport();
                resultFrame.setData(coinReport);
                FastFrame.decoder(resultFrame,recMsg);
                feignClient._03CoinReport(coinReport);

            }
            break;
            case 0x11:{
                _11CardRefundReport cardRefundReport = new _11CardRefundReport();
                resultFrame.setData(cardRefundReport);
                FastFrame.decoder(resultFrame,recMsg);
                feignClient._11CardRefundReport(cardRefundReport);
            }
            break;
            case 0x22:{
                _22CardPreConsumerReport cardPreConsumerReport = new _22CardPreConsumerReport();
                resultFrame.setData(cardPreConsumerReport);
                FastFrame.decoder(resultFrame,recMsg);
                feignClient._22CardPreConsumerReport(cardPreConsumerReport);
            }
            break;
            case 0x33:{
                _33SoftwareReport softwareReport = new _33SoftwareReport();
                resultFrame.setData(softwareReport);
                FastFrame.decoder(resultFrame,recMsg);
                feignClient._33SoftwareReport(softwareReport);
            }
            break;
            case 0x41:{
                _41AlarmWarnReport alarmWarnReport = new _41AlarmWarnReport();
                resultFrame.setData(alarmWarnReport);
                FastFrame.decoder(resultFrame,recMsg);
                feignClient._41AlarmWarnReport(alarmWarnReport);
            }
            break;

            default:
                if (LockContainer.isLock(devId)) {
                    LockContainer.notifyChannel(devId, mqttMessage.getPayload());
                }
                break;

        }
        log.info("接收消息主题:{},接受消息内容:{}", topic, HexBin.bytesToString(mqttMessage.getPayload()));
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
        //log.info("=====deliveryComplete---------" + iMqttDeliveryToken.isComplete());

    }

}
